<template>
  <div class="login-container">
    <h1 style="padding-bottom:18px;">登录</h1>
    <form @submit.prevent="login" v-if="flag">
      <div class="form-group">
        <label for="username">用户名:</label>
        <input type="text" id="username" v-model="loginForm.username" required />
      </div>
      <div class="form-group">
        <label for="password">密码:</label>
        <input type="password" id="password" v-model="loginForm.password" required />
        <div @click.prevent="changeFlag" class="change-flag">邮箱登录</div>
      </div>
      <button type="submit" class="login">登录</button>
      <p class="login-link">
        没有账号？
        <router-link to="/userRegister">前往注册</router-link>.
      </p>
    </form>
    <el-form :model="loginByEmailForm" :rules="rules" ref="loginByEmailForm" v-else>
      <el-form-item label="" prop="email">
        <el-input v-model="loginByEmailForm.email" placeholder="邮箱" style="width: 100%"></el-input>
      </el-form-item>
      <el-form-item label="" prop="captcha">
        <el-input v-model="loginByEmailForm.captcha" placeholder="验证码" style="width: 100%">
          <template slot="append"><el-button
              @click="getEmialCaptcha('loginByEmailForm')">获取</el-button></template></el-input>
        <span @click.prevent="changeFlag" class="change-flag">密码登录</span>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="loginByEmail('loginByEmailForm')">登录</el-button>
        <p class="login-link">
          没有账号？
          <router-link to="/userRegister">前往注册</router-link>.
        </p>
      </el-form-item>
    </el-form>
  </div>
</template>
  
<script>
import {
  getAuthLoginCaptchaByEmail,
  postAuthLogin,
  postAuthLoginByEmail,
} from "../api/controller";
export default {
  data() {
    let flag = true;
    return {
      loginForm: {
        username: "root",
        password: "123456",
      },
      loginByEmailForm: {
        email: "",
        captcha: "",
      },
      flag,
      rules: {
        email: [
          { required: true, message: "请输入邮箱!", trigger: "blur" },
          { type: "email", message: "请输入正确的邮箱!", trigger: "trigger" },
        ],
        captcha: [
          { min: 6, max: 6, message: "验证码应为长度6个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    login() {
      postAuthLogin(this.loginForm)
        .then((response) => {
          const { data } = response;
          this.$message.success("登录成功！");
          localStorage.setItem("token", data.token);
          localStorage.setItem("username", this.loginForm.username);
          this.$router.push(this.$route.query.redirect || "/");
        })
        .catch((error) => {
          this.$message.error(error.message);
        });
    },
    loginByEmail(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postAuthLoginByEmail(this.loginByEmailForm)
            .then((response) => {
              const { data } = response;
              this.$message.success("登录成功！");
              localStorage.setItem("token", data.token);
              localStorage.setItem("username", this.loginForm.username);
              this.$router.push(this.$route.query.redirect || "/");
            })
            .catch((error) => {
              this.$message.error(error.message);
            });
        } else {
          return false;
        }
      });
    },
    getEmialCaptcha(formName) {
      this.$refs[formName].validateField("email", (valid) => {
        if (!valid) {
          const params = {
            email: this.loginByEmailForm.email,
          };
          getAuthLoginCaptchaByEmail(params)
            .then((response) => {
              this.$message.success(response.message);
            })
            .catch((error) => {
              this.$message.error(error.message);
            });
        } else {
          return false;
        }
      });
    },
    changeFlag() {
      this.flag = !this.flag;
    }
  },
};
</script>
  
<style scoped>
.login-container {
  max-width: 400px;
  margin: 200px auto 0;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: white;
}

.login-link {
  margin-top: 18px;
  font-size: 10px;
  /* 调整字号为 14px，可以根据需要进行调整 */
  text-align: center;
  /* 实现居中对齐 */
}

h1 {
  text-align: center;
}

.form-group {
  margin-bottom: 10px;
}

label {
  display: block;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 5px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.login {
  width: 412px;
  margin-top: 28px;
}

button {
  display: block;
  width: 100%;
  padding: 10px;
  margin-top: 10px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}

.change-flag {
  margin-top: 8px;
  cursor: pointer;
  font-size: 14px;
}

.change-flag:hover {
  color: #007bff;
}
</style>
<style>
body {
  margin: 0;
  background-image: url("../assets/background.jpg");
  background-attachment: fixed;
  background-size: 100% 100%;
}
</style>